<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\Images;
use app\common\model\OrderAftersalesRecord;
use think\Db;
use think\Exception;

/**
 * 资金明细
 *
 * @icon fa fa-user
 */
class ShopMoneyRecord extends Backend
{

    protected $relationSearch = true;
    protected $multiFields = 'status';

    protected $model = null;
    protected $agent_id = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('MoneyRecord');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $filter = $this->request->get("filter", '');
            $filter = (array)json_decode($filter, true);
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model->with(['shop','orders'])->where($where)->where(['money_record.member_id'=>0,'money_record.agent_id'=>0,'detail_type'=>['in',[1,4]]])->order($sort, $order)->paginate($limit)->each(function ($item){
                $item['agent_name'] = '';
                if ($item['detail_type'] == 1){//充值
                    $item['agent_name'] = \app\common\model\Agent::where('id',$item['shop']['agent_id'])->value('nickname');
                }elseif ($item['detail_type'] == 4){
                    $item['agent_name'] = \app\common\model\Agent::where('id',$item['orders']['agent_id'])->value('nickname');
                }
                    return $item;
            });

            if (empty($filter['detail_type'])){ //全统计
                $ljcb = $this->model->with(['shop','orders'])->where($where)->where(['money_record.member_id'=>0,'money_record.agent_id'=>0])->where('detail_type',1)->order($sort, $order)->sum('money_record.money');
                $ljfy = $this->model->with(['shop','orders'])->where($where)->where(['money_record.member_id'=>0,'money_record.agent_id'=>0])->where('detail_type',4)->order($sort, $order)->sum('money_record.money');
                $shops = $this->model->with(['shop','orders'])
                    ->where($where)
                    ->where(['money_record.member_id'=>0,'money_record.agent_id'=>0])
                    ->whereIn('detail_type',[1,2])
                    ->group('money_record.shop_id')
                    ->column('money_record.shop_id');

                $all_money = \app\common\model\Shop::whereIn('id',$shops)->sum('money');

                $count = [['name'=>'累计充值金额','value'=>$ljcb],['name'=>'累计余额扣除','value'=>$ljfy],['name'=>'累计剩余金额','value'=>$all_money]];
            }else{ //按类别统计
                $lj_money = $this->model->with(['shop','orders'])->where($where)->where(['money_record.member_id'=>0,'money_record.agent_id'=>0])->where('detail_type',$filter['detail_type'])->order($sort, $order)->sum('money_record.money');

                $shops = $this->model->with(['shop','orders'])
                    ->where($where)
                    ->where(['money_record.member_id'=>0,'money_record.agent_id'=>0])
                    ->where('detail_type',$filter['detail_type'])
                    ->group('money_record.shop_id')
                    ->column('money_record.shop_id');

                $all_money = \app\common\model\Shop::whereIn('id',$shops)->sum('money');


                $arr = ['','累计充值金额','','','累计余额扣除'];
                $name = $arr[$filter['detail_type']];
                $count = [['name'=>$name,'value'=>$lj_money],['name'=>'累计剩余金额','value'=>$all_money]];
            }
            $result = array("total" => $list->total(), "rows" => $list->items(),'extend'=>['count'=>$count]);
            return json($result);
        }
        return $this->view->fetch();
    }

    //订单详情
    public function detail()
    {
        $id = $this->request->param('ids');
        if (!$id) {
            $this->error('参数错误');
        }
        $row = $this->model
            ->where('money_record.id', $id)
            ->with(['shop','orders'])
            ->find();

        if (!$row) {
            $this->error('记录未找到');
        }
        $this->view->assign('row', $row->toArray());
        return $this->view->fetch();
    }



}
